package com.jdk.learn.exam.gui;

import org.junit.Test;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;

/**
 * Created by ricky
 * on 2018/2/27.
 * <p>
 * 顶层容器：
 * JFrame:带有标题，可最大最小化
 * JApplet:内嵌到浏览器中运行
 * JDialog:对话框
 * JWindow:不带标题和控制按钮
 * <p>
 * 面板：
 * JPanel：普通面板
 * JScrollPanel:滚动面板
 */
public class ContainerDemo {
    private JFrame jFrame;

    /**
     * 初始化窗体基本参数
     *
     * @param title
     */
    public void init(String title) {
        jFrame = new JFrame(title);
        jFrame.setBounds(500, 300, 300, 300);
        jFrame.setVisible(true);//设置可见性
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭操作
    }

    /**
     * 内置一个按钮的窗体
     *
     * @throws InterruptedException
     */
    @Test
    public void FirstJFrame() throws InterruptedException {
        init("第一个窗体");
        JButton button = new JButton("点我");
        //等价于下面
       /* JPanel panel=new JPanel();
        panel.setLayout(new BorderLayout());//默认布局是FlowLayout
        panel.add(button,BorderLayout.CENTER);
        jFrame.setContentPane(panel);*/

        jFrame.getContentPane()//内容窗格
                .add(button, BorderLayout.CENTER);//添加按钮组件
        jFrame.pack();//调整框架大小
        Thread.sleep(Integer.MAX_VALUE);
    }


    /**
     * 关于面板的使用
     *
     * @throws InterruptedException
     */
    @Test
    public void FirstJPanel() throws InterruptedException {
        init("面板相关");
        java.awt.Container panel = jFrame.getContentPane();
        panel.setBackground(Color.CYAN);

        JPanel jPanel = new JPanel();//默认是流式布局
        JPanel jPanel2 = new JPanel(new BorderLayout());//可以传递布局管理器实例

        jPanel.setBackground(Color.YELLOW);

        JButton button = new JButton("点我");

        jPanel.add(button);

        panel.add(jPanel, BorderLayout.SOUTH);


        jFrame.pack();//调整框架大小
        Thread.sleep(Integer.MAX_VALUE);
    }

    /**
     * 关于面板的使用2
     *
     * @throws InterruptedException
     */
    @Test
    public void JScrollPanel() throws InterruptedException {
        init("滚动面板相关");
        java.awt.Container panel = jFrame.getContentPane();
//        panel.setBackground(Color.CYAN);

        JScrollPane jScrollPane = new JScrollPane();
        jScrollPane.setAutoscrolls(true);
        JTextArea textArea = new JTextArea(5, 30);
        jScrollPane.setViewportView(textArea);

        panel.add(jScrollPane, BorderLayout.CENTER);
//        jFrame.pack();//调整框架大小
        Thread.sleep(Integer.MAX_VALUE);
    }


    /**
     * 关于面板的使用3
     * 推荐使用方式
     *
     * @throws InterruptedException
     */
    @Test
    public void JScrollPanel2() throws InterruptedException {
        init("滚动面板相关");
        java.awt.Container panel = jFrame.getContentPane();
        JTextArea textArea = new JTextArea(5, 30);
        JScrollPane jScrollPane = new JScrollPane(textArea);
        jScrollPane.setAutoscrolls(true);

        panel.add(jScrollPane, BorderLayout.CENTER);
        Thread.sleep(Integer.MAX_VALUE);
    }


    /**
     * 关于标签的使用
     *
     * @throws InterruptedException
     */
    @Test
    public void JLabel() throws InterruptedException {
        init("标签的使用");
//        Icon icon=new ImageIcon("d:\\apple.png");
        String realpath = System.getProperty("user.dir");
        realpath = realpath + "\\src\\main\\resources\\guiimg\\apple.png";
//        Icon icon=new ImageIcon("guiimage/apple.png");
        Icon icon = new ImageIcon(realpath);

        JLabel label = new JLabel();//默认的空标签
        JLabel label2 = new JLabel("内容");
        JLabel label3 = new JLabel(icon);
        JLabel labelAll = new JLabel("完全体", icon, JLabel.LEFT);

        jFrame.getContentPane().add(labelAll);
        jFrame.pack();//调整框架大小
        Thread.sleep(Integer.MAX_VALUE);
    }

    /**
     * 关于按钮的使用
     *
     * @throws InterruptedException
     */
    @Test
    public void JButton() throws InterruptedException {
        init("按钮");
        JButton button = new JButton("按钮哦");
        final int i = 0;
        button.addActionListener((event) -> {
            System.out.println("您点击了这个按钮");
        });

        jFrame.getContentPane().add(button);
        jFrame.pack();//调整框架大小
        Thread.sleep(Integer.MAX_VALUE);
    }

    /**
     * 关于按钮相关的使用
     *
     * @throws InterruptedException
     */
    @Test
    public void JButtonMore() throws InterruptedException {
        init("按钮相关");
        JToggleButton toggleButton = new JToggleButton("切换按钮1");
        JToggleButton toggleButton2 = new JToggleButton("切换按钮2");
        JToggleButton toggleButton3 = new JToggleButton("切换按钮3");
        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(toggleButton);
        buttonGroup.add(toggleButton2);
        buttonGroup.add(toggleButton3);

        JPanel panel = new JPanel();
        panel.add(toggleButton);
        panel.add(toggleButton2);
        panel.add(toggleButton3);
        jFrame.getContentPane().add(panel);
        jFrame.pack();//调整框架大小
        Thread.sleep(Integer.MAX_VALUE);
    }

    /**
     * 关于按钮相关的使用2
     *
     * @throws InterruptedException
     */
    @Test
    public void JButtonMore2() throws InterruptedException {
        init("按钮相关");
        java.awt.Container container = jFrame.getContentPane();
        JPanel panel = new JPanel();
        //带边框的
        panel.setBorder(BianKuangBorder("复选框"));
        panel.setSize(300, 300);

        JCheckBox checkBox1=new JCheckBox("001");
        JCheckBox checkBox2=new JCheckBox("002");
        JCheckBox checkBox3=new JCheckBox("003");

       /* ButtonGroup groupCheck=new ButtonGroup();
        groupCheck.add(checkBox1);
        groupCheck.add(checkBox2);
        groupCheck.add(checkBox3);*/

        panel.add(checkBox1);
        panel.add(checkBox2);
        panel.add(checkBox3);

        container.add(panel,BorderLayout.NORTH);
        JPanel panel2 = new JPanel();
        //带边框的
        panel2.setBorder(BianKuangBorder("单选框"));
        panel2.setSize(300, 300);

        JRadioButton radioButton=new JRadioButton("001");
        JRadioButton radioButton2=new JRadioButton("002");
        JRadioButton radioButton3=new JRadioButton("003");

        ButtonGroup groupRadio=new ButtonGroup();
        groupRadio.add(radioButton);
        groupRadio.add(radioButton2);
        groupRadio.add(radioButton3);

        panel2.add(radioButton);
        panel2.add(radioButton2);
        panel2.add(radioButton3);
        container.add(panel2,BorderLayout.SOUTH);
        jFrame.pack();//调整框架大小
        Thread.sleep(Integer.MAX_VALUE);
    }

    /**
     * 带边框的组件
     *
     * @param title
     *
     * @return
     */
    public Border BianKuangBorder(String title) {
        Border etched = BorderFactory.createEtchedBorder();
        return BorderFactory.createTitledBorder(etched, title);
    }

    public static void main(String[] args) throws InterruptedException {
    }

}
